﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Edit.aspx.cs" Inherits="MonkeyWarranty.Web.WarrantyTickets.Edit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <title><%=Resources.GlobalStrings.WarrantyTickets %></title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        <%=Resources.GlobalStrings.WarrantyTicketForm %> - <%=Resources.GlobalStrings.WarrantyTickets %></h2>
    <p>
    </p>
    <span class="failureNotification">
        <asp:Literal ID="lblErrorMessage" runat="server"></asp:Literal>
    </span>
    
    <!-- Customer -->
    <div>
        <fieldset class="register">
            <legend><%=Resources.GlobalStrings.Customers%></legend>
            <asp:HiddenField ID="hidCustomerId" runat="server" />
            <p>
            <%=Resources.GlobalStrings.Name %> <asp:TextBox ID="txtName" runat="server" CssClass="textEntry name"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtName"
                        CssClass="failureNotification" ValidationGroup="AddNew">(*)</asp:RequiredFieldValidator>
            <%=Resources.GlobalStrings.Phone %> <asp:TextBox ID="txtPhone" runat="server" CssClass="textEntry name"></asp:TextBox>
            <%=Resources.GlobalStrings.Email %> <asp:TextBox ID="txtEmail" runat="server" CssClass="textEntry name"></asp:TextBox>
            </p>
                <p>
            <%=Resources.GlobalStrings.Address %> <asp:TextBox ID="txtAddress" runat="server" CssClass="textEntry address"></asp:TextBox>
                </p>
        </fieldset>
    </div>
    
    <!-- Common information -->
    <div>
        <fieldset class="register">
            <legend><%=Resources.GlobalStrings.Information %></legend>
            <%=Resources.GlobalStrings.TicketNumber %>
            <asp:TextBox ID="txtCodePrefix" runat="server" Enabled="False" Text="BN" CssClass="textEntry codeprefix"></asp:TextBox> - 
            <asp:TextBox ID="txtCode" runat="server" Enabled="False" CssClass="textEntry codeprefix"></asp:TextBox>
            <%=Resources.GlobalStrings.ReceivedDate %><asp:TextBox ID="txtReceivedDate" runat="server" CssClass="textEntry date"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtReceivedDate"
                        CssClass="failureNotification" ValidationGroup="AddNew">(*)</asp:RequiredFieldValidator>
            <%=Resources.GlobalStrings.DeliveryDate %><asp:TextBox ID="txtDeliveryDate" runat="server" CssClass="textEntry date"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtDeliveryDate"
                        CssClass="failureNotification" ValidationGroup="AddNew">(*)</asp:RequiredFieldValidator>
            <%=Resources.GlobalStrings.ReceivedEmployee %><asp:DropDownList ID="ddlEmployees" runat="server"></asp:DropDownList>
            <p></p>
            <%=Resources.GlobalStrings.ReceivedFrom %>
            <asp:DropDownList ID="ddlReceivedFrom" runat="server">
                <asp:ListItem Value="1" Text="<%$ Resources: GlobalStrings, Customers %>"></asp:ListItem>
                <asp:ListItem Value="2" Text="<%$ Resources: GlobalStrings, Employees %>"></asp:ListItem>
                <asp:ListItem Value="3" Text="<%$ Resources: GlobalStrings, Dealers %>"></asp:ListItem>
            </asp:DropDownList>
            <%=Resources.GlobalStrings.Dealers %><asp:DropDownList ID="ddlDealers" runat="server"></asp:DropDownList><span class="failureNotification" id="dealerError" style="display: none;">(*)</span>
            <%=Resources.GlobalStrings.FromEmployee %><asp:DropDownList ID="ddlFromEmployees" runat="server"></asp:DropDownList><span class="failureNotification" id="fromEmployeeError" style="display: none;">(*)</span>
            <p></p>
            <%=Resources.GlobalStrings.Content %>
            <asp:TextBox ID="txtContent" runat="server" CssClass="textEntry content"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtContent"
                        CssClass="failureNotification" ValidationGroup="AddNew">(*)</asp:RequiredFieldValidator>
                        
            <p></p>
            <%=Resources.GlobalStrings.Status %>
            <asp:DropDownList ID="ddlTicketStatus" runat="server">
            </asp:DropDownList>
            <p></p>

            <div>
                <asp:Button ID="btnSave" runat="server" Text="<%$ Resources:GlobalStrings, Save %>"
                    CssClass="submitButton" ValidationGroup="AddNew" OnClick="btnSave_Click" />
                <input id="btnPrint" type="button" value="<%=Resources.GlobalStrings.Print %>" class="submitButton"  onclick="javascript:window.open('PrintTicket.aspx?id=<%=Request.Params["id"] %>');" />
            </div>
        </fieldset>
    </div>
    <!-- End common -->
    
    <!-- Details -->
    <div>
        <fieldset class="register">
            <legend><%=Resources.GlobalStrings.Details %></legend>
            <div id="tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
                <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
		            <li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active"><a href="#tabs-1"><%=Resources.GlobalStrings.WarrantyProduct%></a></li>
                    <li class="ui-state-default ui-corner-top"><a href="#tabs-2"><%=Resources.GlobalStrings.LendProduct%></a></li>
	            </ul>
                <div id="tabs-1" class="ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide">
		            <div id="ProductsTableContainer"></div>
                    <p>
                        <a href="NewProduct.aspx?ticketId=<%=Request.Params["id"] %>"><%=Resources.GlobalStrings.AddNew%></a>
                    </p>
	            </div>
	            <div id="tabs-2" class="ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide">
		            <div id="LendProductsTableContainer"></div>
                    <p>
                        <a href="NewLendProduct.aspx?ticketId=<%=Request.Params["id"] %>"><%=Resources.GlobalStrings.AddNew%></a>
                    </p>
	            </div>
            </div>
        </fieldset>
    </div>
    <!-- End details -->
    
    <p></p>
    <div>
        <input id="btnCancel" type="button" value="<%=Resources.GlobalStrings.Cancel %>"
                    class="submitButton" onclick="javascript:window.location='List.aspx';" />
    </div>

    <script type="text/javascript">
        $(document).ready(function () {
            $("#<%=txtReceivedDate.ClientID %>").calendar({ timeSeparators: null });
            $("#<%=txtDeliveryDate.ClientID %>").calendar({ timeSeparators: null });
            $("#tabs").tabs();
            
            switch ($("#<%=ddlReceivedFrom.ClientID %>").val()) {
                case '1':
                    $("#<%=ddlDealers.ClientID %>").prop("disabled", true);
                    $("#<%=ddlFromEmployees.ClientID %>").prop("disabled", true);
                    break;
                case '2':
                    $("#<%=ddlDealers.ClientID %>").prop("disabled", true);
                    break;
                case '3':
                    $("#<%=ddlFromEmployees.ClientID %>").prop("disabled", true);
                    break;
            }

            LoadProducts();
            LoadLendProducts();
        });

        $('#<%=ddlReceivedFrom.ClientID %>').change(function () {
            var element = $(this);
            switch (element.val()) {
                case "1":
                    $("#dealerError").hide();
                    $("#fromEmployeeError").hide();
                    $("#<%=ddlDealers.ClientID %>").prop("disabled", true);
                    $("#<%=ddlDealers.ClientID %>").val('0');
                    $("#<%=ddlFromEmployees.ClientID %>").prop("disabled", true);
                    $("#<%=ddlFromEmployees.ClientID %>").val('0');
                    break;
                case "2":
                    $("#<%=ddlDealers.ClientID %>").prop("disabled", true);
                    $("#<%=ddlDealers.ClientID %>").val('0');
                    $("#<%=ddlFromEmployees.ClientID %>").prop("disabled", false);
                    $("#dealerError").hide();
                    $("#fromEmployeeError").show();
                    break;
                case "3":
                    $("#<%=ddlDealers.ClientID %>").prop("disabled", false);
                    $("#<%=ddlFromEmployees.ClientID %>").prop("disabled", true);
                    $("#<%=ddlFromEmployees.ClientID %>").val('0');
                    $("#dealerError").show();
                    $("#fromEmployeeError").hide();
                    break;
                default:
                    break;
            }
        });

        $("#<%=btnSave.ClientID %>").click(function () {
            //e.preventDefault();
            return ValidateInputs();
        });
    </script>
    
    <script type="text/javascript">
        function ValidateInputs() {
            $("#fromEmployeeError").hide();
            $("#dealerError").hide();
            switch ($("#<%=ddlReceivedFrom.ClientID %>").val()) {
                case '2':
                    if ($("#<%=ddlFromEmployees.ClientID %>").val() == '0') {
                        $("#fromEmployeeError").show();
                        return false;
                    }
                    break;
                case '3':
                    if ($("#<%=ddlDealers.ClientID %>").val() == '0') {
                        $("#dealerError").show();
                        return false;
                    }
                    break;
            }

            return true;
        }

        function LoadProducts() {
            $('#ProductsTableContainer').jtable({
                paging: false,
                pageSize: 10,
                sorting: false,
                //defaultSorting: 'Name ASC',
                actions: {
                    listAction: 'Edit.aspx/TicketProductList',
                    deleteAction: 'Edit.aspx/TicketDeleteProduct'
                },
                fields: {
                    Id: {
                        key: true,
                        create: false,
                        edit: false,
                        list: false
                    },
                    Code: {
                        title: '<%=Resources.GlobalStrings.Code %>'
                    },
                    Name: {
                        title: '<%=Resources.GlobalStrings.Name %>'
                    },
                    Model: {
                        title: '<%=Resources.GlobalStrings.Model %>'
                    },
                    ManufacturerName: {
                        title: '<%=Resources.GlobalStrings.Manufacturers %>'
                    },
                    Serial: {
                      title: '<%=Resources.GlobalStrings.Serial %>'  
                    },
                    Quantity: {
                        title: '<%=Resources.GlobalStrings.Quantity %>'
                    },
                    WarrantyTypeName: {
                        title: '<%=Resources.GlobalStrings.WarrantyType %>'
                    },
                    ErrorName: {
                        title: '<%=Resources.GlobalStrings.ErrorCode %>'
                    },
                    WarrantyDate: {
                        title: '<%=Resources.GlobalStrings.WarrantyDate %>',
                        type: 'date',
                        displayFormat: 'dd/mm/yy'
                    },
                    WarrantyProductState: {
                        title: '<%=Resources.GlobalStrings.Status %>'
                    },
                    Edit: {
                        title: '',
                        width: '1%',
                        display: function (data) {
                            return '<input class="jtable-command-button jtable-edit-command-button" onclick="javascript:window.location=\'EditProduct.aspx?id=' + data.record.Id + '\';" />';
                        }
                    }
                }
            });

            $('#ProductsTableContainer').jtable('load', { ticketId: <%=Request.Params["id"] %> });
        }
        
        function LoadLendProducts() {
            $('#LendProductsTableContainer').jtable({
                paging: false,
                pageSize: 10,
                sorting: false,
                //defaultSorting: 'Name ASC',
                actions: {
                    listAction: 'Edit.aspx/TicketLendProductList',
                    deleteAction: 'Edit.aspx/TicketDeleteLendProduct'
                },
                fields: {
                    Id: {
                        key: true,
                        create: false,
                        edit: false,
                        list: false
                    },
                    LendDate: { title: '<%=Resources.GlobalStrings.LendDate %>', type: 'date', displayFormat: 'dd/mm/yy' },
                    EmployeeName: { title: '<%=Resources.GlobalStrings.LendEmployee %>' },
                    WarrantyProductName: { title: '<%=Resources.GlobalStrings.WarrantyProduct %>' },
                    Code: {
                        title: '<%=Resources.GlobalStrings.Code %>'
                    },
                    ProductName: {
                        title: '<%=Resources.GlobalStrings.Name %>'
                    },
                    Model: {
                        title: '<%=Resources.GlobalStrings.Model %>'
                    },
                    ManufacturerName: {
                        title: '<%=Resources.GlobalStrings.Manufacturers %>'
                    },
                    Quantity: {
                        title: '<%=Resources.GlobalStrings.Quantity %>'
                    },
                    LendProductState: {
                        title: '<%=Resources.GlobalStrings.Status %>'
                    },
                    Edit: {
                        title: '',
                        width: '1%',
                        display: function(data) {
                            return '<input class="jtable-command-button jtable-edit-command-button" onclick="javascript:window.location=\'EditLendProduct.aspx?id=' + data.record.Id + '\';" />';
                        }
                    }
                }
            });

            $('#LendProductsTableContainer').jtable('load', { ticketId: <%=Request.Params["id"] %> });
        }
    </script>
</asp:Content>
